package com.nd.android.u.ui.widge;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.common.utils.StarAppUtils;
import com.nd.android.u.allCommonUtils.UrlUtils;
import com.nd.android.u.chatInterfaceImpl.ChatInterfaceImpl;
import com.nd.android.u.controller.ChatConst.MessageContentType;
import com.nd.android.u.controller.ChatGlobalVariable;
import com.nd.android.u.controller.factory.MessageLongClickMenuFactory;
import com.nd.android.u.controller.innerInterface.IChatListItem;
import com.nd.android.u.controller.innerInterface.IMessageDisplay;
import com.nd.android.u.ui.chatUiUtils.ChatCommonUtils;
import com.nd.android.u.ui.chatUiUtils.LongClickableLinkMovementMethod;

/**
 * 聊天界面里的文本消息控件:具体实现在父类
 * Created 2014-5-6 下午2:29:08
 * @version
 * @author cb
 * @see
 */
public class ChatListItemView_Text extends TextView implements IChatListItem{
	
	private IMessageDisplay mMessage;
	private Context mContext;
	
	/**
	 * ChatListItemView_Text.构造方法
	 * <br>Created 2014-9-12 上午11:31:17
	 * @param context
	 */
	public ChatListItemView_Text(Context context) {
		super(context);
		mContext=context;
		StarAppUtils.setViewTypeFace(this, false);
	}
	
	
	/**
	 * ChatListItemView_Text.构造方法
	 * <br>Created 2014-9-12 上午11:31:12
	 * @param context
	 * @param attrs
	 */
	public ChatListItemView_Text(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext=context;
		StarAppUtils.setViewTypeFace(this, false);
	}

	
	@Override
	public void setData(IMessageDisplay message) {
		mMessage=message;
		String text = mMessage.getDisplayContent();
	    if (text == null) {
	        text = "";
	    }
	    setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
	    setVisibility(View.VISIBLE);
		setTextColor(Color.BLACK);
		setTextSize(ChatGlobalVariable.getInstance().getChatTextSize());
		//默认普通消息的链接要强制加SID
		boolean isForceAddSid=true;
		if (mMessage.getMessageContentType()==MessageContentType.PSP_TEXT){
			//公众号的消息做特殊处理
			String tag="sid={sid}";
			int index=text.toLowerCase().indexOf(tag);
			if (index>-1){
				String sid=ChatInterfaceImpl.INSTANCE.chatCallOtherModel.getSid();
				text=text.substring(0, index)+"sid="+sid+text.substring(index+tag.length());
			}
			isForceAddSid=false;
		}
        SpannableStringBuilder ssb = ChatCommonUtils.changeToSpan(text,isForceAddSid);
		setText(ssb);

        URLSpan[] list =  ssb.getSpans(0,ssb.toString().length(), URLSpan.class);

        if (UrlUtils.isHtml(text) && list.length > 0) {
            LongClickableLinkMovementMethod movementmethod = new LongClickableLinkMovementMethod(mlistener);
            setMovementMethod(movementmethod);
            setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Log.e("HYK","event.getAction() = " + event.getAction());
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            setPressed(true);
                            setSelected(true);
                            break;
                        case MotionEvent.ACTION_CANCEL:
                        case MotionEvent.ACTION_UP:
//                        default:
                            setPressed(false);
                            setSelected(false);
                            break;
                    }
                    return false;
                }
            });
        }
        //以下代码是为了解决在某些机型上显示大的图之后再复用的话长宽会和原来的一样大的BUG
		LinearLayout.LayoutParams lp=(LayoutParams) getLayoutParams();
		if (lp==null){
			lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		}else{
			lp.width=LayoutParams.WRAP_CONTENT;
			lp.height=LayoutParams.WRAP_CONTENT;
		}
		lp.weight=1;
		setLayoutParams(lp);
		setFocusable(true);
	}
	
	private OnLongClickListener mlistener=new OnLongClickListener() {
		@Override
		public boolean onLongClick(View v) {
			MessageLongClickMenuFactory.getInstance().show(mContext,mMessage);
			return true;
		}
	};

	@Override
	public IMessageDisplay getData() {
		return mMessage;
	}
}
